package car.aistar.controller;

import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * @ClassName GlobalExceptionHandler
 * @Description TODO
 * @Author laoxu
 * @Date 2020/10/22 20:47
 * Version 1.0
 **/

@SuppressWarnings("all")
@ControllerAdvice
@Controller
public class GlobalExceptionHandler {
    /**
     * 缺少请求参数异常
     *
     * @param ex HttpMessageNotReadableException
     * @return
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(value = HttpStatus.BAD_REQUEST)
    public String handleHttpMessageNotReadableException(
            MissingServletRequestParameterException ex) {
        System.err.println("缺少请求参数，{}"+ ex.getMessage());
        return "error/error400";
//        return new JsonData(400, "缺少必要的请求参数");
    }

    /**
     * 空指针异常
     *
     * @param ex NullPointerException
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleTypeMismatchException(NullPointerException ex) {
        System.err.println(500+ "空指针异常了");
        // return new JsonData();
        return "error/error400";
    }

    /**
     * 系统异常 预期以外异常
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleUnexpectedServer(Exception ex) {
        System.err.println("系统发生异常，请联系管理员");
        return "error/error400";
    }


}
